//
//  LPGTBaseOnlineSchoolViewController.m
//  ShangHaiHuDongApp
//
//  Created by liuyingping on 2018/9/26.
//  Copyright © 2018年 刘应平. All rights reserved.
//

#import "LPGTBaseOnlineSchoolViewController.h"
#import "ZJAdditions.h"
#import "NetWorkingManager.h"
#import "ZJRefurbish.h"
//视频观看
#import "StudyCoureVideoViewController.h"
/** 视频、音频cell */
#import "ZJStudyVideoTableViewCell.h"
/** 图书cell */
#import "ZJStudyBookTableViewCell.h"
#define videoCell @"ZJStudyVideoTableViewCell"
#define bookCell  @"ZJStudyBookTableViewCell"

@interface LPGTBaseOnlineSchoolViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    NSArray *_sqlistDataArray;/**< 本地数据库数据 */
    
}
/** tableView */
@property (nonatomic, strong) UITableView *tableView;
/** dataArray */
@property (nonatomic, strong) NSMutableArray *dataArray;

/** 下滑上滑控价 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
/** 本地数据库数据 */
@property (nonatomic, strong) NSMutableArray *localDataArray;
/**  分页  */
@property (assign, nonatomic) NSInteger  page;
@end

@implementation LPGTBaseOnlineSchoolViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor viewBGGrayColor];
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
        
    }];
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];
    
    [self.tableView.mj_header  beginRefreshing];
    [self setupUI];
    
}

- (void)setupUI{
    [self.view addSubview:self.tableView];
}
//从网上获取数据
- (void)netWorking_header{
    NSDictionary *dict = @{@"PAGE_NUMBER":[NSString stringWithFormat:@"%ld",(long)_page],@"SHOW_NUMBER":@"10",@"CLASSID":self.model.CLASS_ID};
    __weak typeof(self) weakSelf = self;
    [NetWorkingManager postWithUrlString:[NetWorkingURL listCoursewareByClass1] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {//请求成功
            
            weakSelf.dataArray = [[NSArray yy_modelArrayWithClass:[ZJStudySpacialDetailModel class] json:object[@"COURSEWARE_IN_CLASS"]] mutableCopy];
            
            [weakSelf.tableView reloadData];
        }
        [weakSelf.tableView.mj_header endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_header endRefreshing];
    }];
}

//从网上获取数据
- (void)netWorking_footer{
    NSDictionary *dict = @{@"PAGE_NUMBER":[NSString stringWithFormat:@"%ld",(long)_page],@"SHOW_NUMBER":@"10",@"CLASSID":self.model.CLASS_ID};
    __weak typeof(self) weakSelf = self;
    [NetWorkingManager postWithUrlString:[NetWorkingURL listCoursewareByClass1] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {//请求成功
            
            NSArray *array = [NSArray yy_modelArrayWithClass:[ZJStudyRecommendModel class] json:object[@"ST_COURSEWARE"]] ;
            [weakSelf.dataArray addObjectsFromArray:array];
            
            [weakSelf.tableView reloadData];
        }
        [weakSelf.tableView.mj_footer endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}


#pragma mark - UITableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJStudyRecommendModel * model = self.dataArray[indexPath.row];
    if ([model.COURSETYPE isEqualToString:@"4"]) {//悦读会
        ZJStudyBookTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:bookCell forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }else{
        
        ZJStudyVideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:videoCell forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJStudyRecommendModel *model = self.dataArray[indexPath.row];
    if ([model.COURSETYPE isEqualToString:@"4"]){//悦读会
        return 125 * [UIScreen zj_screenHScale];//图书
    }
    return 120 * [UIScreen zj_screenHScale];//视频、音频
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    StudyCoureVideoViewController *vc = [StudyCoureVideoViewController new];
    vc.model = self.dataArray[indexPath.row];
    __weak typeof(self) weakSelf = self;
    vc.block = ^(ZJStudySpacialDetailModel *model) {
        weakSelf.dataArray[indexPath.row] = model;
        [weakSelf.tableView reloadData];
    };
    [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
}

#pragma mark - Setter

#pragma mark - Getters
- (UITableView *)tableView{
    if (_tableView == nil){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - 44 - LP_TopHeight) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        if (self.isStudy) {
            _tableView.height = self.view.height - 44 - LP_TopHeight - 49;
        }
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 7.5 * [UIScreen zj_screenHScale])];
        view.backgroundColor = [UIColor viewBGGrayColor];
        _tableView.tableHeaderView = view;
        [_tableView  registerClass:[ZJStudyVideoTableViewCell class] forCellReuseIdentifier: videoCell];
        [_tableView  registerClass:[ZJStudyBookTableViewCell class] forCellReuseIdentifier: bookCell];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray{
    if (_dataArray == nil){
        _dataArray = [NSMutableArray array];
        
    }
    return _dataArray;
}

- (NSMutableArray *)localDataArray{
    if (_localDataArray == nil){
        _localDataArray = [NSMutableArray array];
        
    }
    return _localDataArray;
}

- (ZJRefurbish *)refurbish{
    if (_refurbish == nil){
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}

@end
